package com.CST.service.impl;

import com.CST.dto.SysRoleMenuDTO;
import com.CST.entity.BdSysRoleMenu;
import com.CST.mapper.cst.BdSysRoleMenuMapper;
import com.CST.service.IBdSysRoleMenuService;
import com.CST.util.UUIDUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 角色菜单  服务实现类
 * </p>
 *
 * @author: y
 * @create: 2021-09-28 09:11
 **/
@Service
public class BdSysRoleMenuServiceImpl extends ServiceImpl<BdSysRoleMenuMapper, BdSysRoleMenu> implements IBdSysRoleMenuService {

    /**
     * <p>新建角色菜单</p >
     *
     * @param sysRoleMenuDto
     * @author y
     * @since 2021/09/28 09:42
     */
    @Override
    public boolean insertSysRoleMenu(SysRoleMenuDTO sysRoleMenuDto) {
        boolean flag = true;
        for (String menuId : sysRoleMenuDto.getMenuId()) {
            BdSysRoleMenu sysRoleMenu = new BdSysRoleMenu();
            sysRoleMenu.setId(UUIDUtil.uuid());
            sysRoleMenu.setRoleId(sysRoleMenuDto.getRoleId());
            sysRoleMenu.setMenuId(menuId);
            sysRoleMenu.setCreateTime(new Date());
            sysRoleMenu.setDelFlag("0");
            flag = flag && save(sysRoleMenu);
        }
        return flag;
    }

    /**
     * <p>删除角色菜单</p >
     *
     * @param id
     * @return boolean
     *
     * @author y
     * @since 2021/09/28 10:19
     */
    @Override
    public boolean deleteSysRoleMenuById(String id) {
        BdSysRoleMenu rm = getSysRoleMenuById(id).setDelFlag("0");
        if (Objects.isNull(rm)) {
            return false;
        }
        BdSysRoleMenu bdSysRoleMenu = selectById(id);
        bdSysRoleMenu.setDelFlag("1");
        return updateById(bdSysRoleMenu);
    }

    /**
     * <p>更新角色菜单</p >
     *
     * @param sysRoleMenuDto
     * @return
     *
     * @author y
     * @since 2021/09/28 10:16
     */
    @Override
    public boolean updateSysRoleMenu(SysRoleMenuDTO sysRoleMenuDto) {
        baseMapper.deleteBySysMenuId(sysRoleMenuDto.getRoleId());
        return insertSysRoleMenu(sysRoleMenuDto);
    }

    /**
     * <p>查询</p >
     *
     * @param id
     * @return com.CST.entity.BdSysRoleMenu
     *
     * @author y
     * @since 2021/09/28 10:36
     */
    @Override
    public BdSysRoleMenu getSysRoleMenuById(String id) {
        return baseMapper.getSysRoleMenuById(id);
    }

    /**
     * <p>根据角色或菜单查询</p >
     *
     * @param roleId, menuId
     * @return java.util.List<com.CST.entity.BdSysRoleMenu>
     *
     * @author y
     * @since 2021/09/28 14:22
     */
    @Override
    public List<BdSysRoleMenu> getSysRoleMenu(String roleId, String menuId) {
        return baseMapper.getSysRoleMenu(roleId, menuId);
    }


    private BdSysRoleMenu selectById(String id) {
        return baseMapper.selectById(id);
    }
}
